package com.my.mygaode

import android.graphics.Bitmap
import android.text.TextUtils
import android.util.Log
import android.view.View
import com.amap.api.maps.AMap
import com.amap.api.maps.model.*
import com.amap.api.maps.model.animation.Animation
import com.amap.api.maps.model.animation.ScaleAnimation
import com.my.mygaode.bean.MarkerBean
import com.my.mygaode.view.MapContainerView

/**
 * @ClassName MarkerHelper
 * @Description
 * @Author mwj
 * @Date 2023/2/17 10:48
 * @Version 1.0
 **/
class MarkerHelper {

    private var aMap: AMap

    /**
     * 缓存所有添加marker信息配置集合
     */
    private var markerInfoList = ArrayList<MarkerBean>()
        private set

    /**
     * 缓存map地图上显示的marker集合
     */
    private var markerMapList = ArrayList<Marker>()

    /**
     * marker的点击事件
     */
    private var clickMarker: (MarkerBean?) -> Unit = { }

    constructor(aMap: AMap) {
        this.aMap = aMap
        aMap.setOnMarkerClickListener {
            val markerBean = getMarkerToMarkerBean(it)
            clickMarker(markerBean)
            return@setOnMarkerClickListener true
        }
    }

    private var showMarkerList = ArrayList<Marker>()

    private var hideMarkerList = ArrayList<Marker>()

    //只有为true时候才给marker带有动画效果显示
    var isUpdateShowMapMarkerAnimation = false

    /**
     * 更新当前屏幕上显示的marker的集合信息
     */
     fun updateShowMapMarkerListInfo() {
        val list = aMap.mapScreenMarkers
        showMarkerList.clear()
        hideMarkerList.clear()
        if (list.isNullOrEmpty()) {
            addNoSelfMarkerToList(hideMarkerList, markerMapList)
            markerMapList.clear()
        } else {
            if (markerMapList.isNullOrEmpty()) {
                markerMapList.addAll(list!!)
                addNoSelfMarkerToList(showMarkerList, markerMapList)
            } else {
                for (newBean in list!!) {
                    var isHave = markerMapList.contains(newBean)
                    if (isHave) {
                        continue
                    }
                    markerMapList.add(newBean)
                    if (!checkSelfMarker(newBean)) {
                        showMarkerList.add(newBean)
                    }
                }
                var iterator = markerMapList.iterator()
                while (iterator.hasNext()) {
                    val bean = iterator.next()
                    var isHave = list.contains(bean)
                    if (isHave) {
                        continue
                    }
                    if (!checkSelfMarker(bean)) {
                        hideMarkerList.add(bean)
                    }
                    iterator.remove()
                }
            }
        }
        if (!isUpdateShowMapMarkerAnimation){
            return
        }
//        LogUtils.i(LogUtils.TAG_DATA,"showMarkerList--->"+showMarkerList.size+","+showMarkerList.toString())
        showMarkerList.forEach {
            setScaleAnimation(it, true)
        }
//        LogUtils.i(LogUtils.TAG_DATA,"hideMarkerList--->"+hideMarkerList.size+","+hideMarkerList.toString())
        hideMarkerList.forEach {
            setScaleAnimation(it, false)
        }
        showMarkerList.clear()
        hideMarkerList.clear()
    }

    private fun setScaleAnimation(marker: Marker, isShow: Boolean) {
        val markerAnimation = if (isShow) {// 缩放比例
            ScaleAnimation(0f, 1f, 0f, 1f)
        } else {
            ScaleAnimation(1f, 0f, 1f, 0f)
        }
        markerAnimation.setDuration(500) // 动画时间
        // 动画之后保存的状态 0 动画之后的状态 1 动画之前的状态
        markerAnimation.fillMode = Animation.FILL_MODE_FORWARDS
        marker?.setAnimation(markerAnimation) // 将动画赋值给当前的marker
        marker?.startAnimation()
    }

    private fun checkSelfMarker(marker: Marker): Boolean {
        var markerBean = getMarkerIdToMarkerBean(marker.id)
        if (TextUtils.equals(markerBean?.id, MapContainerView.MY_LOCATION_ID)) {
            return true
        }
        return false
    }

    private fun addNoSelfMarkerToList(newList: ArrayList<Marker>, oldList: ArrayList<Marker>) {
        for (bean in oldList) {
            if (checkSelfMarker(bean)) {
                continue
            }
            newList.add(bean)
        }
    }


    /**
     * 图片地址
     * @param absolutePath String?
     * @return Marker?
     */
    fun addMarkerBean(
        id: String,
        absolutePath: String?,
        latLng: LatLng,
        isAdd: Boolean = true
    ): MarkerBean {
        val marker = checkMarkerBean(id, isAdd)
        marker.markerOptions.position(latLng)
        marker.markerOptions.icon(BitmapDescriptorFactory.fromPath(absolutePath))
        return marker
    }

    /**
     * 图片地址
     * @param absolutePath String?
     * @return Marker?
     */
    fun addMarkerBean(
        id: String,
        bitmap: Bitmap?,
        latLng: LatLng,
        isAdd: Boolean = true
    ): MarkerBean {
        val marker = checkMarkerBean(id, isAdd)
        marker.markerOptions.position(latLng)
        marker.markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
        return marker
    }


    /**
     * 添加一个自定义view的覆盖物
     * @param id String
     * @param view View
     * @param latLng LatLng
     * @param isAdd Boolean
     * @return MarkerOptions
     */
    fun addMarkerBean(id: String, view: View, latLng: LatLng, isAdd: Boolean = true): MarkerBean {
        val marker = checkMarkerBean(id, isAdd)
        marker.markerOptions.icon(BitmapDescriptorFactory.fromView(view))
        marker.markerOptions.position(latLng)
        return marker
    }

    /**
     * 得到一个缓存MarkerBean ，没有就会创建一个MarkerBean
     * @param id String 作为根据得到缓存list里面的MarkerBean
     * @param isAdd Boolean 是否立即加入到地图上显示
     * @return MarkerBean
     */
   private fun checkMarkerBean(id: String, isAdd: Boolean = true): MarkerBean {
        var markerOptions: MarkerBean? = getIdToMarkerBean(id)
        if (markerOptions == null) {
            markerOptions = MarkerBean(id, MarkerOptions())
            if (isAdd) {
                addMarkerBean(markerOptions)
            }
        }
        return markerOptions
    }


    fun getIdToMarkerBean(id: String): MarkerBean? {
        var markerBean: MarkerBean? = null
        for (bean in markerInfoList) {
            if (TextUtils.equals(id, bean.id)) {
                markerBean = bean
                break
            }
        }
        return markerBean
    }

    fun getMarkerIdToMarkerBean(markerId: String): MarkerBean? {
        var markerBean: MarkerBean? = null
        for (bean in markerInfoList) {
            if (TextUtils.equals(markerId, bean.markerId)) {
                markerBean = bean
                break
            }
        }
        return markerBean
    }

    /**
     * 得到显示marker信息
     * @param markerId String?
     * @return Marker?
     */
    fun getShowMarker(markerId: String?): Marker? {
        if (TextUtils.isEmpty(markerId)) return null
        var marker: Marker? = null
        markerMapList.forEach {
            if (TextUtils.equals(markerId, it.id)) {
                marker = it
                return@forEach
            }
        }
        return marker
    }

    /**
     * 添加一个覆盖物
     * @param marker MarkerOptions
     */
    fun addMarkerBean(marker: MarkerBean?) {
        marker ?: return
        Log.d("mygaode", "已添加一个Marker。。。。")
        markerInfoList.add(marker)
    }

    /**
     * 清除除marker数据
     */
    fun clearMarkers() {
        markerInfoList.clear()
    }

    /**
     * 释放资源
     */
    fun release(){
        aMap.setOnMarkerClickListener(null)
        clearMarkers()
        markerMapList.clear()
    }

    /**
     * 多条MarkerOptions加入
     * @param list List<MarkerOptions>?
     */
    fun addMarkerBeanList(list: List<MarkerBean>?) {
        if (!list.isNullOrEmpty()) {
            markerInfoList.addAll(list)
        }
    }

    /**
     * 显示所有marker
     */
    fun showMarkers() {
        aMap.clear()
//        var list = ArrayList<MarkerOptions>()
//        var markerList = markerInfoList
//        markerList.forEach { list.add(it.markerOptions) }
//        //第二个参数，为true的时候，会把对应marker坐标放缩到中心显眼的地方显示，默认false
//
//        var mapList = aMap?.addMarkers(list, false)
//        markerMapList.clear()
//        if (!mapList.isNullOrEmpty()) {
//            markerMapList.addAll(mapList)
//        }
        markerInfoList.forEach {
            val marker = aMap.addMarker(it.markerOptions)
            it.markerId = marker?.id
        }
        updateShowMapMarkerListInfo()
    }

    /**
     * 通过marker得到markerInfoList里面对应的MarkerBean信息
     * @param marker Marker?
     * @return MarkerBean?
     */
    fun getMarkerToMarkerBean(marker: Marker?): MarkerBean? {
        var markerBean: MarkerBean? = null
        markerInfoList.forEach {
            if (TextUtils.equals(it.markerId, marker?.id)) {
                markerBean = it
                return@forEach
            }
        }
        return markerBean
    }

    fun setClickMarkerListener(listener: (MarkerBean?) -> Unit) {
        this.clickMarker = listener
    }

//    /**
//     * 得到地图上一个marker
//     * @param bean MarkerBean
//     * @return Marker?
//     */
//    fun getMarkerMap(bean: MarkerBean?): Marker? {
//        bean ?: return null
//        var marker: Marker? = null
//        markerMapList.forEach {
//            if (it.options == bean.markerOptions) {
//                marker = it
//                return@forEach
//            }
//        }
//        return marker
//    }
}